package com.itzyf.pokemondata.activity

import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import com.itzyf.pokemondata.R
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_damage_calculation.*


/**
 * @author 依风听雨
 * @version 创建时间：2019/01/02 10:07
 */
class DamageCalculationActivity : BaseActivity() {
    override fun handlerActivity() {
        spAttackAdd.setSelection(6)
        spDefensiveAdd.setSelection(6)
    }

    override fun getContentViewId() = R.layout.activity_damage_calculation

    override fun getToolbarTitle() = "伤害计算器"

    private fun calculate(
        mega: Double,
        level: Int,
        power: Double,
        attack_power: Long,
        defensive_power: Long,
        restraint: Double,
        department: Double,
        crit: Int,
        prop: Double
    ) {

        val result =
            (mega * ((level * 2.0 / 5 + 2) * power *
                    attack_power / defensive_power / 50 + 2) * restraint * department * prop * crit)

        showSnackbar("计算结果：${Math.round(0.793 * result)} ~ ${Math.round(result)}")
    }

    fun submit() {
        val level = etLevel.text.toString()
        //0 是  1否
        val mega = spMega.selectedItemPosition
        //招式威力
        val power = etPower.text.toString()
        //攻击力
        val attackPower = etAttackPower.text.toString()
        //攻击加成
        val attackAdd = spAttackAdd.selectedItemPosition
        //防御力
        val defensivePower = etDefensivePower.text.toString()
        //防御加成
        val defensiveAdd = spDefensiveAdd.selectedItemPosition
        //是否是本系，0非本，1本系，2本系适应力
        val depart = spDepart.selectedItemPosition
        //克制系数
        val restraint = spRestraint.selectedItemPosition
        //是否会心，0否，1会心，2狙击手会心
        val crit = spCrit.selectedItemPosition
        //其他
        val other = etOther.text.toString()
        //道具
        val propS = etProp.text.toString()

        if (TextUtils.isEmpty(level)) {
            showToast("请输入攻击方等级")
            return
        }
        if (TextUtils.isEmpty(power)) {
            showToast("请输入攻击方技能威力")
            return
        }
        if (TextUtils.isEmpty(attackPower)) {
            showToast("请输入攻击方攻击力")
            return
        }
        if (TextUtils.isEmpty(defensivePower)) {
            showToast("请输入防御方防御力")
            return
        }
        if (TextUtils.isEmpty(propS)) {
            showToast("请输入道具加成，无加成输入1")
            return
        }
        if (TextUtils.isEmpty(other)) {
            showToast("请输入其他加成，无加成输入1")
            return
        }
        var attack = attackPower.toLong()
        val intArray = resources.getStringArray(R.array.addition)
        val s = intArray[attackAdd].toInt()
        attack = if (s >= 0) {
            Math.round(attack + attack * s * (1.0 / 3))
        } else {
            Math.round(attack * (3.0 / (Math.abs(s) + 3)))
        }

        var defensive = defensivePower.toLong()
        val s1 = intArray[defensiveAdd].toInt()
        defensive = if (s1 >= 0) {
            Math.round(defensive + defensive * s1 * (1.0 / 3))
        } else {
            Math.round(defensive * (3.0 / (Math.abs(s1) + 3)))
        }
//        println("攻击力：$attack;防御力：$defensive")
        calculate(
            if (mega == 0) 34.64 else 26.0,
            level.toInt(),
            power.toDouble(),
            attack,
            defensive,
            when (restraint) {
                1 -> 1.5
                2 -> 2.0
                3 -> 3.0
                4 -> 4.0
                5 -> 0.5
                6 -> 0.25
                else -> 1.0
            },
            when (depart) {
                1 -> 1.5
                2 -> 2.0
                else -> 1.0
            },
            when (crit) {
                1 -> 2
                2 -> 3
                else -> 1
            }, other.toDouble() * propS.toDouble()
        )
    }

    override
    fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.submit, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == R.id.action_sumbit) {
            submit()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}